<?php
/**
 * SHOP 控制台首页
 * ============================================================================
 * 网络科技有限公司，并保留所有权利。
*/
define('IN_ZYS', true);
require(dirname(__FILE__) . '/includes/init.php');
if($_REQUEST['act'] == 'list'){
    $cat_id=isset($_REQUEST['cat_id'])?$_REQUEST['cat_id']:0;
    if($cat_id){
        //搜索分类
        $sql="select * from ".ly()."article_cat where cat_id='{$cat_id}'";
        $parent_id=$cat_id;
    }else{
        //查询文章分类列表
        $sql="select * from ".ly()."article_cat";
        $parent_id="";
    }
    /*进入列表页*/
	$num='15';
	$res=$db->query($sql);
	$total_count=$db->num_rows($res);
	$pagelist = new page(array('total'=>$total_count, 'perpage'=>$num));
	$currenpage=$pagelist->nowindex;
	$start=($currenpage-1)*$num;
	if ($total_count>$num){
		$smarty->assign('page',$pagelist->show(3));
	}else{
		$smarty->assign('page','');
	}
	$limit=" limit ".$start.','.$num;
	/*分页结束*/
	$sql.=$limit;
    $row=$db->getAll($sql);
    $sql_type="select value from ".ly()."shop_config where code='cat_type'";
    $row_type=$db->getOne($sql_type);
    $data=unserialize($row_type);
    foreach ($row as $key => &$value) {
       foreach ($data as $k => $v) {
            if($value['cat_type']==$k){
                $value['cat_type']=$v;
            }
       }
    }
    //获取文章分类
    $sql="select * from ".ly()."article_cat";
    $type=$db->getAll($sql);
    $smarty->assign('type',news_type_cat($parent_id,$type));
    $smarty->assign('list',$row);
    $smarty->display('news_type_list.html');
}elseif($_REQUEST['act']=='add'){
    $cat_id=isset($_REQUEST['cat_id'])?$_REQUEST['cat_id']:0;
    if($cat_id){
        $sql="select * from ".ly()."article_cat where cat_id='{$cat_id}'";
        $res=$db->getRow($sql);
		$parent_id=$res['parent_id'];
        $smarty->assign('edit',$res); 
    }else{
	    $parent_id="";
	}
	$sql="select * from ".ly()."article_cat";
	$type=$db->getAll($sql);
	$smarty->assign('type',news_type_cat($parent_id,$type));
	$smarty->display('news_type_edit.html');
}elseif($_REQUEST['act']=='edit'){
    $cat_id=isset($_POST['cat_id'])?$_POST['cat_id']:0;
    $cat_name=isset($_POST['cat_name'])?$_POST['cat_name']:0;
    $parent_id=isset($_POST['parent_id'])?$_POST['parent_id']:0;
    $sort_order=isset($_POST['sort_order'])?$_POST['sort_order']:0;
    $keywords=isset($_POST['keywords'])?$_POST['keywords']:0;
    $cat_desc=isset($_POST['cat_desc'])?$_POST['cat_desc']:0;
    if($cat_id>0){
        $sql="update ".ly()."article_cat set cat_name='{$cat_name}',parent_id='{$parent_id}',sort_order='{$sort_order}',keywords='{$keywords}',cat_desc='{$cat_desc}' where cat_id='{$cat_id}'";
    }else{
        $sql="insert into ".ly()."article_cat(cat_name,parent_id,sort_order,keywords,cat_desc) values('{$cat_name}','{$parent_id}','{$sort_order}','{$keywords}','{$cat_desc}')";
    }
    $db->query($sql);
    if(@mysql_affected_rows){
         $href[]=array('text'=>"返回文章分类列表",'href'=>'news_type.php?act=list');
         sys_msg("操作成功！",$href);
     }else{
        $href[]=array('text'=>"重新编辑",'href'=>'history.back()');
        sys_msg("操作失败！",$href,fsssssssalse);
     }
}elseif($_REQUEST['act']=='delete'){
    //删除文章分类
    $cat_id=isset($_POST['cat_id'])?$_POST['cat_id']:0;
    $sql="select cat_id from ".ly()."article_cat where parent_id='{$cat_id}'";
    $res=$db->getOne($sql);
    if($res){
        echo "3";
    }else{
        $sql="delete from ".ly()."article_cat where cat_id='{$cat_id}'";
         $db->query($sql);
         if(@mysql_affected_rows()){
            echo "1";
         }else{
            echo "2";
         }
    }
}elseif($_REQUEST['act']=='type_judge'){
    $cat_id=isset($_REQUEST['cat_id'])?$_REQUEST['cat_id']:0;
    $str=get_typeId();
    $str=',1'.$str.',';
    $cat_id=','.$cat_id.',';
    $abc=strstr($str,$cat_id);
    if($abc){
        echo "2";
    }else{
        echo "1";
    }
}
//系统分类及所有子分类的ID
function get_typeId($cat_id=1){
    $str="";
    $sql="select cat_id from ".ly()."article_cat where parent_id='{$cat_id}'";
    $res=$GLOBALS['db']->getAll($sql);
    foreach ($res as $k => $v) {
       if($v['cat_id']){
            $str.=','.$v['cat_id'];
            $str.=get_typeID($v['cat_id']);
         } 
    }   
    return $str;
}
/*新闻分类筛选调用*/
function news_type_cat($p_id=false,$arr){
	$array=array();
	$array=$arr;
	$str="";
	foreach($arr as $v){
		if($v['parent_id']<1){
			if($p_id==$v['cat_id']){$select=" selected='selected'";}else{$select="";}
			$str.= "<option  ".$select." value='".$v['cat_id']."'>".$v['cat_name']."</option>";
			$str.= news_type_arr($v['cat_id'],$array,$p_id);
		}
	}
	return $str;
}
/*新闻分类子级调用*/
function news_type_arr($cat_id,$arr,$p_id=false,$nbsp=false){
	$array=array();
	$array=$arr;
	$str="";
	if($nbsp){
		$nbsp.="&nbsp;&nbsp;&nbsp;&nbsp;";
	}else{
		$nbsp="&nbsp;&nbsp;&nbsp;&nbsp;";
	}
	foreach($arr as $v){
		if($cat_id==$v['parent_id']){
			if($p_id==$v['cat_id']){$select=" selected='selected'";}else{$select="";}
			$str.= "<option ".$select." value='".$v['cat_id']."'>".$nbsp.$v['cat_name']."</option>";
			$str.= news_type_arr($v['cat_id'],$array,$p_id,$nbsp);
		}
	}
	return $str;
}